package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.pojo.Situation;
import com.cl.service.impl.SituationServiceImpl;
import com.cl.utils.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SituationController {
    @Autowired
    private SituationServiceImpl situationService;
    @Autowired
    private MsgUtil msgUtil;
    @PostMapping("/isSituation")
    public String isSituation(@RequestParam("form")String form, @RequestParam("sNo")String s){
        int sNo = Integer.parseInt(s);
        JSONObject form1 = JSON.parseObject(form);
        String date = (String) form1.get("date");
        String describe = (String) form1.get("describe");
        String yes = (String) form1.get("yes");
        int i = situationService.addSituation(sNo, date, describe,yes);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("添加成功"));
        }
        return JSON.toJSONString(msgUtil.fail("添加失败"));
    }
    @PostMapping("/getSituation")
    public String getSituation(@RequestParam("sNo")String s){
        int sNo = Integer.parseInt(s);
        Situation situation = situationService.queryBySno(sNo);
        if (situation!=null){
            return JSON.toJSONString(situation);
        }
        return JSON.toJSONString(msgUtil.fail("未发生异常"));
    }

    @GetMapping("/getSituation")
    public String getSituation(@RequestParam(value = "name",required = false)String name,@RequestParam("page") int page,@RequestParam("limit") int limit,@RequestParam(value = "yon",required = false)String yon){
        List<Map<String, Object>> maps;
        int size = situationService.countAll();
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",size);
        map.put("code",0);
        map.put("msg","");
        if (name==null&&yon==null){
            maps = situationService.querySituation((page - 1) * limit, limit,null,null);
            map.put("data",maps);
            return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
        }else if (yon == null){
            maps = situationService.querySituation((page - 1) * limit, limit,name,null);
            map.put("data",maps);
            return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
        }else {
            maps = situationService.querySituation((page - 1) * limit, limit,null,yon);
            map.put("data",maps);
            return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
        }
    }
    @GetMapping("/getSituationNumber")
    public String getSituationNumber(){
        HashMap<String,Integer> map = new HashMap<>();
        int abnormal = situationService.countAbnormal();
        int normal = situationService.countNormal();
        map.put("abnormal",abnormal);
        map.put("normal",normal);
        return JSON.toJSONString(map);
    }
}
